package com.simen.connectandroid.command;

import com.simen.connectandroid.command.utils.CommandCreator;
import com.simen.connectandroid.model.Device;
import com.simen.hellobytes.StringUtil;
import com.simen.hellobytes.runtime.RuntimeHelper;

/**
 * 从设备中提取文件到本机
 * Created on 2021/11/18 13:00.
 *
 * @author Simen
 */
public class PullCommand extends AdbCommand<PullCommand> {

    private boolean commandResult;
    private String filePath;
    private String outFileName;

    public PullCommand(String filePath) {
        this.filePath = filePath;
    }

    public PullCommand(Device device, String filePath) {
        super(device);
        this.filePath = filePath;
    }

    @Override
    public PullCommand exec() {
        if (StringUtil.isTrimEmpty(filePath)) {
            System.out.println("filePath can't be empty");
            return this;
        }

        RuntimeHelper runtimeHelper = RuntimeHelper.create(CommandCreator.createAdb()
                .appendAdbDevice(device)
                .appendParam("pull " + filePath, outFileName)
                .build());
        runtimeHelper.exec();

        commandResult = runtimeHelper.getProcessCode() == 0;
        log = runtimeHelper.getOutputLog();

        return this;
    }

    @Override
    public boolean execResult() {
        return commandResult && log.contains(" pulled");
    }

    public PullCommand setOutFileName(String outFileName) {
        this.outFileName = outFileName;
        return this;
    }

    public String getFilePath() {
        return filePath;
    }

    public String getOutFileName() {
        return outFileName;
    }
}
